package pers.zxk.jlytnews.dao.impl;

import pers.zxk.jlytnews.dao.UserDao;
import pers.zxk.jlytnews.entity.User;
import pers.zxk.jlytnews.util.DBconn;
import pers.zxk.jlytnews.util.DateUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {

    private static final String TABLE_NAME = "tb_userinfo";
    private static final String COL_USERNAME = "username";
    private static final String COL_PASSWORD = "password";
    private static final String COL_NICKNAME = "nickname";
    private static final String COL_GENDER = "gender";
    private static final String COL_ADMIN = "admin";
    private static final String COL_DEPT = "dept";
    private static final String COL_PHONE = "phone";
    private static final String COL_ICON = "icon";
    private static final String COL_LASTDATE = "lastdate";
    private static final String COL_ID = "id";

    @Override
    public boolean addUser(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        int i = DBconn.addUpdDel("insert into " + TABLE_NAME + "(" + COL_USERNAME + "," + COL_PASSWORD + "," +
                COL_NICKNAME + "," + COL_GENDER + "," + COL_ADMIN + "," + COL_DEPT + "," + COL_PHONE + "," +
                COL_ICON + "," + COL_LASTDATE + ") " +
                "values('" + user.getUsername() + "','" + user.getPassword() + "','" + user.getNickname() + "','" +
                user.getGender() + "','" + user.getAdmin() + "','" +
                user.getDept() + "','" + user.getPhone() + "','" + user.getIcon() + "','" + user.getLastdate() + "')");
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean delUser(String username) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from " + TABLE_NAME + " where " + COL_USERNAME + "=" + username;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateUser(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_PASSWORD + " ='" + user.getPassword()
                + "' , " + COL_NICKNAME + " =" + user.getNickname()
                + "' , " + COL_GENDER + " =" + user.getGender()
                + "' , " + COL_ADMIN + " =" + user.getAdmin()
                + " , " + COL_DEPT + " =" + user.getDept()
                + " , " + COL_ICON + " ='" + user.getIcon()
                + "' , " + COL_PHONE + " ='" + user.getPhone()
                + "' , " + COL_ICON + " ='" + user.getIcon()
                + "' , " + COL_LASTDATE + " =" + user.getLastdate()
                + "' where " + COL_USERNAME + " = " + user.getUsername();
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public User login(User user) throws SQLException, ClassNotFoundException {
        DBconn.init();
        User userInfo = null;
        //todo 使用pstmt 提高安全性
        String sql = "select * from " + TABLE_NAME + " where " + COL_USERNAME + " = '" + user.getUsername() +
                "' and " + COL_PASSWORD + " = '" + user.getPassword() + "'";
        ResultSet rs = DBconn.selectSql(sql);
        if (rs.next()) {
            userInfo = new User();
            userInfo.setId(rs.getInt(COL_ID));
            userInfo.setUsername(rs.getString(COL_USERNAME));
            userInfo.setNickname(rs.getString(COL_NICKNAME));
            userInfo.setGender(rs.getString(COL_GENDER));
            userInfo.setAdmin(rs.getInt(COL_ADMIN));
            userInfo.setDept(rs.getString(COL_DEPT));
            userInfo.setPhone(rs.getString(COL_PHONE));
            userInfo.setIcon(rs.getString(COL_ICON));
            userInfo.setLastdate(rs.getInt(COL_LASTDATE));//获取上一次登录日期
            updateLastDate(user);//更新最后一次登录日期为当前时间
        }
        DBconn.closeConn();
        return userInfo;
    }

    @Override
    public boolean updateLastDate(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_LASTDATE + " = " + (int) (new Date().getTime() / 1000)
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean checkUserName(String username) throws SQLException, ClassNotFoundException {
        DBconn.init();
        String sql = "select * from " + TABLE_NAME + " where " + COL_USERNAME + " = '" + username + "'";
        ResultSet rs = DBconn.selectSql(sql);
        if (rs.next()) {
            DBconn.closeConn();
            return false;//重名了！！！
        } else {
            DBconn.closeConn();
            return true;
        }
    }

    @Override
    public boolean isAdmin(User user) throws SQLException, ClassNotFoundException {
        return false;
    }

    @Override
    public boolean registerUser(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "insert into " + TABLE_NAME + " (" + COL_USERNAME + "," + COL_PASSWORD + "," + COL_ADMIN + ") " +
                "values('" + user.getUsername() + "','" + user.getPassword() + "','" + user.getAdmin() + "')";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public User getUserInfo(User user) throws SQLException, ClassNotFoundException {
        DBconn.init();
        ResultSet rs = DBconn.selectSql("select * from " + TABLE_NAME + " where " + COL_USERNAME + " = " + user.getUsername());
        if (rs.next()) {
            user.setId(rs.getInt(COL_ID));
            user.setPassword(rs.getString(COL_PASSWORD));
            user.setUsername(rs.getString(COL_USERNAME));
            user.setNickname(rs.getString(COL_NICKNAME));
            user.setAdmin(rs.getInt(COL_ADMIN));
            user.setDept(rs.getString(COL_DEPT));
            user.setIcon(rs.getString(COL_ICON));
            user.setPhone(rs.getString(COL_PHONE));
            user.setLastdate(rs.getInt(COL_LASTDATE));
            DBconn.closeConn();
            return user;
        } else {
            DBconn.closeConn();
            throw new SQLException("未找到id为" + user.getUsername() + "的数据");
            //return null;
        }
    }

    @Override
    public List<User> getUserList(int page, int size) throws SQLException, ClassNotFoundException {
        return null;
    }

    @Override
    public boolean updatePassword(String userName, String oldPassword, String newPassword) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_PASSWORD + " = '" + newPassword + "'" +
                " where " + COL_USERNAME + " = '" + userName + "' and " + COL_PASSWORD + " = '" + oldPassword + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateNickname(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_NICKNAME + " = '" + user.getNickname() + "'" +
                " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateGender(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_GENDER + " = '" + user.getGender() + "'"
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateAdmin(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();

        String sql = "update " + TABLE_NAME + " set " + COL_ADMIN + " = " + user.getAdmin()
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updatePhone(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_PHONE + " = '" + user.getPhone() + "'"
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateDept(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_DEPT + " = '" + user.getDept() + "'"
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateIcon(User user) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_ICON + " = '" + user.getIcon() + "'"
                + " where " + COL_USERNAME + " = '" + user.getUsername() + "'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }
}
